package com.itheima.reggie.common;

import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice  //  标注这是一个rest风格的异常处理器 也可以分开为@ResponseBody @ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 异常处理方法
     *
     */

    @ExceptionHandler(BusinessException.class)
    public R<String> businessExceptionHandler(BusinessException ex){
        log.error(ex.getMessage());
//            if(ex.getMessage().contains("Duplicate entry")){
//                String[] split = ex.getMessage().split(" ");
//                String msg=split[2]+"已存在";
//                return R.error(msg);
//            }
        // ex.getMessage();
        return R.error(ex.getMessage());

    }

    @ExceptionHandler(SystemException.class)
    public R<String> systemExceptionHandler(SystemException ex){

        return R.error(ex.getMessage());

    }


    @ExceptionHandler(Exception.class)
    public R<String> ExceptionHandler(Exception ex){

        return R.error(ex.getMessage());

    }
}
